﻿
@{
    ViewData["Title"] = "Index";
    Layout = "~/Areas/Admin/Views/_Layout.cshtml";

    var OrgTypeList=ViewBag.OrgTypes as List<Mrc.Entity.SysOrgType>;
}
<link href="~/lib/layui/layui_ext/dtree/dtree.css" rel="stylesheet" />
<link href="~/lib/layui/layui_ext/dtree/font/dtreefont.css" rel="stylesheet" />
@*<script src="~/lib/layui/layui_ext/dtree/dtree.js"></script>*@
<div class="layui-container" style="width:100%">
    <div class="layui-row">
        <div class="layui-col-md3" style="height:900px;overflow: auto;margin-top:1em">
            <ul id="demoTree" class="dtree" data-id="0" style=""></ul>
        </div>
        <div class="layui-col-md9" style="margin-top:1em;width:54%">
            <form class="layui-form layui-form-pane" id="formdata">
                <div class="layui-form-item">
                    <label class="layui-form-label">组织名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">所属组织</label>
                        <div class="layui-input-block">
                            <select name="type" lay-filter="myOrg">
                                @foreach (var type in OrgTypeList)
                                {
                                    <option value="@type.Id">@type.Name</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">上级</label>
                        <div class="layui-input-block">
                            <select name="parentId" id="parentId"></select>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">描述</label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入内容" id="description" name="description" class="layui-textarea"></textarea>
                    </div>
                </div>
                <input type="text" name="id" value="" id="id" class="layui-input layui-hide">
                <input type="text" name="permissionIds" id="permissionIds" value="" class="layui-input layui-hide">
                <div class="layui-form-item">
                    <button class="layui-btn" type="button" lay-submit="" lay-filter="add">添加</button>
                    <button class="layui-btn" type="button" lay-submit="" lay-filter="update">更新</button>
                    <button class="layui-btn" type="button" id="setPermission" lay-filter="setPermission">权限配置</button>
                </div>
            </form>
        </div>
    </div>
</div>
<script>layui.extend({
        dtree: '/lib/layui/layui_ext/dtree/dtree'
    }).use(['element', 'layer', 'dtree', 'form'], function () {
        var element = layui.element,
            layer = layui.layer,
            dtree = layui.dtree,
            form = layui.form,
            $ = layui.$;
        var DTree1 = dtree.render({
            elem: "#demoTree",
            url: "/admin/org/models",
            dot: false,
            toolbar: true,
            toolbarScroll: "#demoTree",
            toolbarLoad: "leaf",  // 表示工具栏加载的节点，  可选的值还有：noleaf（非最后一级）,leaf（最后一级节点）
            toolbarShow: ["delete"],
            toolbarFun: {
                delTreeNode: function (treeNode) {
                    var nowNode = dtree.getNowParam("demoTree");
                    $.post("/admin/org/delete", { id: nowNode.nodeId} , function (repo) {
                        showAndReload(layer, repo);
                    });
                }
            }
        });

        dtree.on("node('demoTree')", function (obj) {
            var data = obj.param;
            $("#id").val('');
            $("#formdata").initForm($.parseJSON(data.basicData));
            form.render();
            var parent = dtree.getParentParam(DTree1, data.nodeId + '');
            $("#ParentName").val(parent.context);
        });


        $("#setPermission").click(function (){
            layer.open({
                type: 1,
                title: "设置权限",
                area: ["500px", "80%"],
                content: '<ul id="selectParent" class="dtree" data-id="0"></ul>',
                btn: ['确认设置'],
                success: function (layero, index)
                {
                    var DTree3 = dtree.render({
                        obj: $(layero).find("#selectParent"),
                        url: "/admin/org/GetPermissionTree?id=" + $("#id").val(),
                        initLevel: 5,
                        checkbar: true // 开启复选框
                    });
                },
                yes: function (index, layero) {
                    var flag = true;
                    var params = dtree.getCheckbarNodesParam("selectParent"); // 获取选中值
                    if (params.length == 0) {
                        layer.msg("请至少选择一个节点", { icon: 2 });
                        flag = false;
                    }
                    var ids = [];
                    for (var key in params)
                    {
                        var param = params[key];
                        ids.push(param.nodeId);
                    }
                    var id = $("#id").val();
                    if (id != "")
                    {
                        BaseAjax(ajaxType.post, "/admin/org/SetPermission", { id: id, permissions: ids },function(repo){
                            if (flag) {
                                layer.close(index);
                            }
                        });
                    }
                    else
                    {
                        $("#permissionIds").val(ids.join(','));
                        layer.close(index);
                    }
                }
            });
        });

        form.on('submit(update)', function (data)
        {
            BaseAjax(ajaxType.post, "/admin/org/update", data.field, function (repo) {
                if (repo.status == ajaxStatus.OK)
                {
                    window.location.reload();
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        form.on('submit(add)', function (data) {
            BaseAjax(ajaxType.post,"/admin/org/add", data.field,function(repo){
              window.location.reload();
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });


        form.on('select(myOrg)', function (data) {
            $.get("/admin/org/GetParentOrgs", { orgType: data.value }, function (repo) {
                var html = '';
                Object.keys(repo.data).forEach(function (item) {
                {
                        html += '<option value="' + repo.data[item].id + '">' + repo.data[item].name + '</option>'
                }});
                $("#parentId").html(html);
                form.render('select'); 
            })
        });  

    })</script>